<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Users extends MY_Admin
{
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function Users()
	{
		parent::MY_Admin();
		
		// Load required classes
		$this->load->model('mdl_users', 'users');
		
		// Template Settings
		$auth = $this->config->item('auth');
		
		foreach($auth as $key => $value)
		{
			$this->$key = $value;
		}
			
	}

	// --------------------------------------------------------------------
	
	/**
	 * List all users
	 *
	 * @access	public
	 */
	function index()
	{
		$this->load->library('pagination');

        $config['base_url'] = site_url('admin/users/index');
        $config['total_rows'] = $this->users->user_count();
        $config['per_page'] = '20';
        $config['uri_segment'] = 4;
       // $config['num_links'] = 2;
        $config['first_link'] = '<< Awal';
        $config['last_link'] = 'Akhir >>';
        $config['next_link'] = 'Selanjutnya >';
        $config['prev_link'] = '< Sebelumnya';
        
        $this->pagination->initialize($config);     
	    $data['users'] = $this->users->get_users($config['per_page'],$this->uri->segment(4));		
		$data['content'] = $this->load->view('admin/users/list', $data, TRUE);
		
		$this->load->view('admin/users/list', $data);
	}
	
	function edit($id)
	{
		// Only logged out users
		$this->load->library('validation');
		$this->load->helper('form');
		
		// Set rules
		$rules = array(
				'email'	 		=>	'trim|required|valid_email|check_email',
				'username'		=>	'trim|required|strip_tags|xss_clean|max_length[15]|check_username',
				'password'		=>	'trim|required',
				'p_confirm'		=>	'trim|required|matches[password]',
				'name'			=>	'trim|required|strip_tags|xss_clean|max_length[50]',
				'location'		=>	'trim|required|strip_tags|xss_clean|max_length[50]',
				'telpon'		=>	'trim|required|strip_tags|xss_clean|max_length[50]',
				'company'		=>	'trim|required|strip_tags|xss_clean|max_length[50]'
			);
		$this->validation->set_rules($rules);
		
		// Set fields
		$fields = array(
				'email'	 		=>	'e-mail',
				'username'		=>	'username',
				'password'		=>	'password',
				'p_confirm'		=>	'password confirmation',
				'name'			=>	'last name',
				'location'		=>	'location',
				'telpon'		=>	'telpon',
				'company'		=>	'company',				
				'type'		    =>	'type'
		);
		$this->validation->set_fields($fields);
		
		// Run the validation
		if ($this->validation->run() == FALSE)
		{
			$data['id'] = $id;
			$this->db->where('user_id',$id);
			$result=$this->db->get("users");
			$data['data']=$result->row();
			
			//get all group 
			//var_dump($data);
			$this->load->view('admin/users/edit', $data);
		}
		else
		{
			$userdata = array(
					'name'			=>	$this->input->post('name'),
					'location'		=>	$this->input->post('location'),
					'company'		=>	$this->input->post('company'),
					'telpon'		=>	$this->input->post('telpon')
					//'active'		=>	'1'
			);
						
			//$register = $this->access->register($userdata);
			$this->db->where('user_id',$id);
			$this->db->update("users",$userdata);
			$data['id'] = $id;
			$this->db->where('user_id',$id);
			$result=$this->db->get("users");
			$data['data']=$result->row();
			$this->load->view('admin/users/edit', $data);
		}
	}
	
    function add()
	{
	 	$this->load->library('validation');
		$this->load->helper('form');
		
		// Set rules
		$rules = array(
				'email'	 		=>	'trim|required|valid_email|check_email',
				'username'		=>	'trim|required|strip_tags|xss_clean|max_length[15]|check_username',
				'password'		=>	'trim|required',
				'p_confirm'		=>	'trim|required|matches[password]',
				'name'			=>	'trim|required|strip_tags|xss_clean|max_length[50]',
				'location'		=>	'trim|required|strip_tags|xss_clean|max_length[50]',
				'telpon'		=>	'trim|required|strip_tags|xss_clean|max_length[50]',
				'company'		=>	'trim|required|strip_tags|xss_clean|max_length[50]'
			);
		$this->validation->set_rules($rules);
		
		// Set fields
		$fields = array(
				'email'	 		=>	'e-mail',
				'username'		=>	'username',
				'password'		=>	'password',
				'p_confirm'		=>	'password confirmation',
				'name'			=>	'name',
				'location'		=>	'location',
				'telpon'		=>	'telpon',
				'company'		=>	'company'			
		);
		$this->validation->set_fields($fields);
		
		// Run the validation
		if ($this->validation->run() == FALSE)
		{
			$this->load->view('admin/users/add');
		}
		else
		{
			$userdata = array(
					'name'		    =>	$this->input->post('name'),
					'username'		=>	$this->input->post('username'),
					'email'			=>	$this->input->post('email'),
					'password'		=>	$this->input->post('password'),
					'location'		=>	$this->input->post('location'),
					'company'		=>	$this->input->post('company'),
					'telpon'		=>	$this->input->post('telpon'),
					'active'		=>	'1'
							);
						
			$register = $this->access->register($userdata);
			
			switch($register)
			{
				case 'REGISTRATION_SUCCESS':
					$this->session->set_flashdata('msg', 'Add User Complete.');
					redirect('admin/users');
				case 'REGISTRATION_SUCCESS_EMAIL':
					$this->session->set_flashdata('msg', 'Registration complete, a confirmation email has been sent!');
					redirect('admin/users');
				default:
					show_error('Registration failed');
			}
		}
	}
    function active()
	{
		$this->load->library('pagination');

        $config['base_url'] = site_url('admin/users/index');
        
        $this->db->like('active', '1');
        $this->db->from('users');
        $config['total_rows'] = $this->db->count_all_results();;
        $config['per_page'] = '20';
        $config['uri_segment'] = 4;
       // $config['num_links'] = 2;
        $config['first_link'] = '<< Awal';
        $config['last_link'] = 'Akhir >>';
        $config['next_link'] = 'Selanjutnya >';
        $config['prev_link'] = '< Sebelumnya';
        
        $this->pagination->initialize($config);     
	    $data['users'] = $this->users->get_users($config['per_page'],$this->uri->segment(4),1);		
		$data['content'] = $this->load->view('admin/users/list', $data, TRUE);
		
		$this->load->view('admin/users/list', $data);
	}
    function inactive()
	{
		$this->load->library('pagination');

        $config['base_url'] = site_url('admin/users/index');
        
        $this->db->like('active', '0');
        $this->db->from('users');
        $config['total_rows'] = $this->db->count_all_results();;
        $config['per_page'] = '20';
        $config['uri_segment'] = 4;
       // $config['num_links'] = 2;
        $config['first_link'] = '<< Awal';
        $config['last_link'] = 'Akhir >>';
        $config['next_link'] = 'Selanjutnya >';
        $config['prev_link'] = '< Sebelumnya';
        
        $this->pagination->initialize($config);     
	    $data['users'] = $this->users->get_users($config['per_page'],$this->uri->segment(4),0);		
		$data['content'] = $this->load->view('admin/users/list', $data, TRUE);
		
		$this->load->view('admin/users/list', $data);
	}
    function ban()
	{
		$this->load->library('pagination');

        $config['base_url'] = site_url('admin/users/index');
        
        $this->db->like('active', '2');
        $this->db->from('users');
        $config['total_rows'] = $this->db->count_all_results();;
        $config['per_page'] = '20';
        $config['uri_segment'] = 4;
       // $config['num_links'] = 2;
        $config['first_link'] = '<< Awal';
        $config['last_link'] = 'Akhir >>';
        $config['next_link'] = 'Selanjutnya >';
        $config['prev_link'] = '< Sebelumnya';
        
        $this->pagination->initialize($config);     
	    $data['users'] = $this->users->get_users($config['per_page'],$this->uri->segment(4),2);		
		$data['content'] = $this->load->view('admin/users/list', $data, TRUE);
		
		$this->load->view('admin/users/list', $data);
	}
	// --------------------------------------------------------------------
	
	/**
	 * Remove a user
	 *
	 * @param	integer	id of the user to remove
	 * @access	public
	 */
	function remove($id)
	{
	//	confirm('Do you really want to delete this user and all his entries?');
				
		// Superadmin id == 1
		if ($id === '1')
		{
			// @TODO: Style have to go in a CSS stylesheet, not mixed in code
			$this->session->set_flashdata('msg', '<b style="color: #800;">Superadmin cannot be deleted</b>');
			redirect('admin/users');
		}
		else if ($this->user->id !== '1' AND $this->access->get_group($id) === 'Administrators')
		{
			show_error('You do not have permission to delete other Administrators.');
		}
		
		
		$this->users->delete($id);
		
		$this->session->set_flashdata('msg', 'User Deleted');
		redirect('admin/users');
	}
	
	function banned($id)
	{
	    $this->users->banned($id);
	    $this->session->set_flashdata('msg', 'User Banned');
	    $data['msg_view']	= 'banned';
		
	    $message = $this->load->view('email/template', $data, TRUE);

	    //get email
	    $this->db->where('user_id',$id);
		$result=$this->db->get("users");
		$hasil=$result->row();
		$this->email->from($this->mail_from_email, $this->mail_from_name);
		$this->email->to($hasil->email);
		$this->email->subject("UKMBOGOR - Banned account");	
		$this->email->message($message);
		$this->email->send();
		redirect('admin/users');
	}
    function activate($id)
	{
	    $this->users->unbanned($id);
	    $this->session->set_flashdata('msg', 'User Is active');
	    $this->load->library('email');
	    $data['msg_view']	= 'activated';
		
	    $message = $this->load->view('email/template', $data, TRUE);

	    //get email
	    $this->db->where('user_id',$id);
		$result=$this->db->get("users");
		$hasil=$result->row();
		$this->email->from($this->mail_from_email, $this->mail_from_name);
		$this->email->to($hasil->email);
		$this->email->subject("UKMBOGOR activation email");	
		$this->email->message($message);
		$this->email->send();
		redirect('admin/users');
	}
    function unbanned($id)
	{
	    $this->users->unbanned($id);
	    $this->session->set_flashdata('msg', 'User Banned');
	    $data['msg_view']	= 'unbanned';
		
	    $message = $this->load->view('email/template', $data, TRUE);

	    //get email
	    $this->db->where('user_id',$id);
		$result=$this->db->get("users");
		$hasil=$result->row();
		$this->email->from($this->mail_from_email, $this->mail_from_name);
		$this->email->to($hasil->email);
		$this->email->subject("UKMBOGOR - unbanned email");	
		$this->email->message($message);
		$this->email->send();
		redirect('admin/users');
	}
	
}

/* End of file users.php */
/* Location: ./application/controllers/admin/users.php */